package cn.com.self.security.service;


import cn.com.self.security.authentication.SecurityUser;

import cn.com.self.security.mapper.MenuRightMapper;
import cn.com.self.security.mapper.RoleMapper;
import cn.com.self.security.user.domain.MenuRight;
import cn.com.self.security.user.domain.Role;
import cn.com.self.security.user.domain.LoginRoleEntity;
import cn.com.self.security.user.dto.user.SysUserEntity;
import cn.com.self.security.user.dto.user.UserDTO;
import cn.com.self.security.user.service.ILoginRoleService;
import cn.com.self.security.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component("securityUserService")
public class SecurityUserService implements UserDetailsService {
    @Autowired
    private UserService userService;
    @Resource
    private RoleMapper roleMapper;
    @Resource
    private MenuRightMapper menuRightMapper;
    @Autowired
    private ILoginRoleService sysRoleService;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        SysUserEntity sysUserEntity= userService.getSysUserByUsername(username);
        if (null != sysUserEntity){
            Map<String, Object> queryParams = new HashMap<String, Object>();
            queryParams.put("userid",sysUserEntity.getId());
            List<LoginRoleEntity> dataList = sysRoleService.selectRoleList(queryParams);
            sysUserEntity.setRoles(dataList);
            String roles = "";
            for(LoginRoleEntity sys : dataList){
                roles += ","+ sys.getName();
            }
            List<GrantedAuthority> authorities = AuthorityUtils.commaSeparatedStringToAuthorityList("".equals(roles) ? "": roles.substring(1));
            SecurityUser securityUser = new SecurityUser(SysUserEntity.user2LoginSysUser(sysUserEntity));
            //SecurityUser securityUser = new SecurityUser(LoginUserDTO.user2LoginUserDTO(userDTO));
            return new User(sysUserEntity.getUsername(), sysUserEntity.getPassword(), authorities);
            //return securityUser;
        }else {
            throw new UsernameNotFoundException(username + " 用户不存在!");
        }
        /*UserDTO userDTO = userService.getRolesByUsername(username);
        // 默认用户ID为1的为管理员
        if (null != userDTO){
            if(1L == userDTO.getId()) {
                this.getAdminPermission(userDTO);
            }
            SecurityUser securityUser = new SecurityUser(LoginUserDTO.user2LoginUserDTO(userDTO));
            return securityUser;
        } else {
            throw new UsernameNotFoundException(username + " 用户不存在!");
        }*/
    }

    /**
     * 为管理员赋所有权限
     * @param userDTO
     * @return
     */
    private UserDTO getAdminPermission(UserDTO userDTO) {
        List<Role> roles = roleMapper.selectAll();
        List<MenuRight> menuRights = menuRightMapper.selectAll();
        userDTO.setRoles(roles);
        userDTO.setMenus(menuRights);
        return userDTO;
    }
}
